Skip to main content

[CS50] Week1 C

1. Hello World

#include <stdio.h>

int main(void)
{
printf("Hello, World\n");
}

2. Header file vs Library

這兩者的差異,還沒弄的很清楚,先貼上搜集到的資訊

2.1 Difference between Header file and Library

(Ref: https://www.tutorialspoint.com/difference-between-header-file-and-library )

danger

注意是stdio.h 而不是studio.h

3.Functions

The statement at the start of the code #include <stdio.h> is a very special command that tells the compile that you want to use the capabilities of library called stdio.h. This allows you, among many other things, to utilize the printf function.

4. Variables

4.1 What’s %s

    #include <cs50.h>
#include <stdio.h>

int main(void)
{
string answer = get_string("What's your name? ");
printf("hello, %s\n", answer);
}
  • Notice that #include <cs50.h> has been added to the top of your code. The get_string function is used to get a string from the user. Then, the variable answer is passed to the printf function. %s tells the printf function to prepare itself to receive a string.

  • answer is a special holding place we call a variableanswer is of type string and can hold any string within it.

  • There are many data types, such as intboolchar, and many others.

  • Running make hello again in the terminal window, you can run your program by typing ./hello. The program now asks for your name and then says hello with your name attached.

5. Conditionals

#include <cs50.h>
#include <stdio.h>

int main(void)
{
// Prompt user to agree
char c = get_char("Do you agree? ");

// Check whether agreed
if (c == 'Y' || c == 'y')
{
printf("Agreed.\n");
}
else if (c == 'N' || c == 'n')
{
printf("Not agreed.\n");
}
}
  • User story

    • 詢問user是否同意?
    • if 'Y' ot 'y' => printf("Agreed.\n")
    • elase if 'N' or 'n' printf("Not agreed.\n")
  • Notice that single quotes are utilized for single characters. Further, notice that == ensure that something is equal to something else, where a single equal sign would have a very different function in C. Finally, notice that || effectively means or.

  • You can test your code by typing make agree into the terminal window, followed by ./agree.

6. Loops

6.1 The cat meow 3 times

#include <stdio.h>

int main(void)
{
printf("meow\n");
printf("meow\n");
printf("meow\n");
}

6.2 improve by loop

#include <stdio.h>

int main(void)
{
int i = 0;
while (i < 3)
{
printf("meow\n");
i++;
}
}

6.3 implement a count-down of sorts

#include <stdio.h>

int main(void)
{
int i = 3;
while (i > 0)
{
printf("meow\n");
i--;
}
}

6.4 We can even loop forever

#include <cs50.h>
#include <stdio.h>

int main(void)
{
while (true)
{
printf("meow\n");
}
}

7. Single / Double quotation

7.1 Single quotation

7.2 Double quotation

8. Convention

8.1 indent by 4 spaces

9. Integers overflow

9.1 floating-point imprecision

1/3應該是0.333循環,但是由於bits不足,沒有足夠的位元來存放小數點後的數字,因此造成小數點後的位數被截斷,程式強迫進位,導致了結果的不準確性。

10. How to fix integers overflow?

10.1 Double (not perfect enough)

10.2 To be continued

11. What's the issue in real world?

11.1 Year 2000 problem

兩千年問題始於1960年代,當時電腦記憶體和外部儲存媒介的成本很高,大多數資料處理需要藉助穿孔卡片。為了節省硬體成本,葛麗絲·霍普在Harvard Mark I上以6位數字來儲存時間,即年、月、日各兩位。這個習慣被COBOL繼承下來,傳播到整個電腦程式界。

因此,當年份只用兩位數字來記錄時,當從1999 年 跨至 2000年時便會產生 integers overflow的錯誤。

(由於 integers overflow的錯誤,電子看板將2000年1月3日錯誤顯示為了1900年1月3日)